package com.ztercelstudio.demo002

import android.util.Log
import com.ztercelstudio.demo002.Person.classInherit
import com.ztercelstudio.demo002.Singleton.index


class Person1(name: String, age: Int) {  // 构造 + init
    init {
        Log.d("Demo002", "Person1 - name: $name age: $age");
    }
}

class Person2(name: String, age: Int) {  // 构造参数与成员定义
    var mName = "12"
    init {
        Log.d("Demo002", "Person2 - name: $name age: $age mName: $mName");
        mName = name
    }

    fun print() {
        Log.d("Demo002", "Person2::print - mName: $mName"/*name: $name    age: $age")*/)
    }
}

class Person3(var name: String, var age: Int) { // 构造函数中定义成员变量
    var mName = "12"
    init {
        mName = name
    }

    fun print() {
        Log.d("Demo002", "Person3::print -name: $name  mName: $mName  age: $age");
    }
}

class Person4 {
    init {
        Log.d("Demo002", "Person4 - init");
    }

    constructor(name: String) {
        Log.d("Demo002", "Person4 - name: $name");
    }

    constructor(name: String, age: Int) {  // 次函数中var定义， 成员变量
        Log.d("Demo002", "Person4 - name: $name age: $age");
    }

    constructor(name: String, age: Int, className: String = "class"): this(name, age) {  // 调用另一个次构造函数
        Log.d("Demo002", "Person4 - name: $name age: $age class: $className");
    }
}

fun classDeclare() {

    Person1("zTercel", 21);

    val p2 = Person2("zTercel", 23)
    p2.print()
    Log.d("Demo002", "Person2 - mName ${p2.mName}")   //p2.name 不是在构造函数中定义的var是非成员变量

    val p3 = Person3("yiguigen", 23)
    p3.print()
    p3.let {
        it.age = 33
        it.name = "zTercel"
        it.mName = "modified"
    }
    p3.print()

    Person4("andrich")
    Person4("yiguigen", 22);
    Person4("zTercel", 23, "class2");
}

fun dataClass() {
    data class User(var name: String, var age: Int, var sex: Int, var isActive: Boolean = false);

    var user1 = User("zTercel", 19, 0, true);
    Log.d("Demo002", "user1: $user1")

    var user2 = User("yiUigen", 91, 0, true);
    Log.d("Demo002", "user2: $user2")
    user2.let{
        it.name = "YiGuiGen"
        it.sex = 1
        it.age = 24
        it.isActive = true
    }
    Log.d("Demo002", "user2: $user2")
}

object Singleton {
    private var index = 0;

    init {
        index++;
        Log.d("Demo002", "Singleton::init - index: $index")
    }

    fun operate() {
        Log.d("Demo002", "Singleton::operate - index: $index")
    }

}

fun singleClass() {
    Singleton.operate();
    Singleton.operate();
}
//-------------------------------------------------
enum class Status { // 默认值
    SUCCESS,
    FAILED
}

fun check(status: Status): String {
    return when(status) {
        Status.SUCCESS -> "成功"
        Status.FAILED -> "失败"
        else -> "unknow"
    }
}

enum class Color(var value: Int) {  // 指定值
    RED(0xFF0000),
    BLUE(0X0000FF),
    GREEN(0X00FF00)
}

enum class Type {
    TEACHER {
        override fun action(): String = "教书"
    },

    DOCTOR {
        override fun action(): String = "治病"
    };
    abstract fun action(): String
}

fun search(e: Type) {
    for (item in enumValues<Type>()) {
        if (item == e) {
            Log.d("Demo002", "${item.name} - action ${item.action()}")
            break;
        }
    }
}

fun enumClass() {
    val status: Status = Status.FAILED
    Log.d("Demo002", "enumClass - ${check(status)} status: ${status.name} ${status.ordinal}")

    val list = enumValues<Color>()
    for (item in list) {
        Log.d("Demo002", "enumClass - ${item.name}  ${String.format("0x%06X", item.value)}")
    }

    search(Type.DOCTOR)
}

fun sampleEntry() {
    Log.d("Demo002", "-------- 类定义 -----------")
    classDeclare()

    Log.d("Demo002", "-------- 类继承 -----------")
    classInherit()

    Log.d("Demo002", "-------- 数据类 -----------")
    dataClass()

    Log.d("Demo002", "-------- 单例类 -----------")
    singleClass()

    Log.d("Demo002", "-------- 枚举类 -----------")
    enumClass()

}